package com.ruoyi.hr.controller.hr;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.hr.domain.Jobs;
import com.ruoyi.hr.service.CompanysService;
import com.ruoyi.hr.service.DepartmentsService;
import com.ruoyi.hr.service.JobsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Controller
@RequestMapping("/job")
public class JobController extends BaseController {
    @Autowired
    private JobsService jobsService;
    @Autowired
    private DepartmentsService departmentsService;

    @Autowired
    private CompanysService companysService;

    @Autowired
    private CompanyController companyController;

    private String prefix = "hr/job";
    /**
     * 跳转分页页面
     */
    @RequiresPermissions("hr:job:view")
    @GetMapping()
    public String user() {
        return prefix + "/job";
    }

    /**
     * 获取分页数据以及数据总量
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Jobs job) {
        int PageNum = getNum(); //取得页数
        int pageSize = getSize(); //取得显示数
        List<Jobs> list = null; //分页List
        int PageCount;//总数据量
        //List<String> AllID = companyController.getJuriID(null);
        //PageCount = jobsService.findCount(job,AllID);
        //list = jobsService.findAll(PageNum, pageSize, job,AllID);
        //return getDataTable1(list, PageCount);
        return null;
    }


    /**
     * 修改
     *
     * @param JobID 主键值
     */
    @RequiresPermissions("hr:job:edit")
    @GetMapping("/edit/{JobID}")
    public String edit(@PathVariable("JobID") Long JobID, ModelMap mmap) {
        Jobs jobs = jobsService.findJobsOne(JobID);
        mmap.put("Allcompanys", companysService.findAllCompanys(null));//获取所有公司
        mmap.put("DepartmentAll", departmentsService.findDep(jobs.getCompanyID().longValue()));//获取这个公司下的所有部门
        if(jobs.getAdminDepID() == null || jobs.getAdminDepID() == 0){
            mmap.put("JobsAll", jobsService.findJobs(new Long(jobs.getDepID())));//获取上级岗位所在的部门
        }else{
            mmap.put("JobsAll", jobsService.findJobs(new Long(jobs.getAdminDepID())));//获取上级岗位所在的部门
        }
        mmap.put("jobs",jobs);
        return prefix + "/edit";
    }


    /**
     * 修改岗位
     */
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated Jobs jobs) {
        return toAjax(jobsService.updateJobs(jobs));
    }


    /**
     * 新增岗位跳转
     */
    @RequiresPermissions("hr:job:add")
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("Allcompanys", companysService.findAllCompanys(null));//获取所有公司
        mmap.put("DepartmentAll", departmentsService.findDep(new Long(33)));//获取控股部门
        return prefix + "/add";
    }


    /**
     * 新增岗位
     */
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated Jobs jobs) {
        return toAjax(jobsService.insertJobs(jobs));
    }


    /**
     * 查看岗位
     */
    @RequiresPermissions("hr:job:detail")
    @GetMapping("/detail/{JobID}")
    public String detailSave(@PathVariable("JobID") Long JobID, ModelMap mmap) {
        Jobs jobs = jobsService.findJobsOne(JobID);
        mmap.put("jobs",jobs);
        return prefix + "/detail";
    }

}
